package com.google.appengine.api.datastore;

import java.util.List;

import com.google.appengine.api.datastore.DataTypeTranslator.Type;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.apphosting.api.DatastorePb.CompiledCursor;
import com.google.storage.onestore.v3.OnestoreEntity.Property;
import com.google.storage.onestore.v3.OnestoreEntity.PropertyValue;
import com.google.storage.onestore.v3.OnestoreEntity.Reference;

/**
 * Fix problem wit package access restriction without interfering the original
 * source.
 * 
 * @author Jakub Dykowski
 * 
 */
public class AccessibilityHelper {

	public static long ARBITRARY_FAILOVER_READ_MS = DatastoreServiceImpl.ARBITRARY_FAILOVER_READ_MS;

	public static Comparable<Object> getComparablePropertyValue(Object value) {
		return DataTypeTranslator.getComparablePropertyValue(value);
	}

	// TODO implement properly, find usage and correct if necessary
	public static Query cloneQueryWithFilters(Query query,
			List<FilterPredicate> filters) {
		return new Query(query.getKind(), query.getAncestor(),
				query.getSortPredicates(), query.getFilter(), filters,
				query.isKeysOnly(), query.getAppIdNamespace(),
				query.getProjections(), query.getDistinct(),
				query.getFullTextSearch());
	}

	public static String getAppId(Query query) {
		return query.getAppIdNamespace().getAppId();
	}

	public static AppIdNamespace getAppIdNamespace(Query query) {
		return query.getAppIdNamespace();
	}

	public static Cursor newCursor(Cursor cursor) {
		return new Cursor(cursor);
	}

	public static Cursor newCursor() {
		return new Cursor();
	}

	public static void advance(Cursor cursor, int n, PreparedQuery query) {
		cursor.advance(n, query);
	}

	public static FetchOptions newFetchOptions(FetchOptions opts) {
		return new FetchOptions(opts);
	}

	public static FetchOptions clearOffset(FetchOptions opts) {
		return opts.clearOffset();
	}

	public static FetchOptions clearLimit(FetchOptions opts) {
		return opts.clearLimit();
	}

	public static FetchOptions limit(FetchOptions opts, int limit) {
		return opts.limit(limit);
	}

	public static FetchOptions compile(FetchOptions opts, boolean bool) {
		return opts.compile(bool);
	}

	public static Boolean getCompile(FetchOptions opts) {
		return opts.getCompile();
	}

	public static Property toProperty(String property, Object value) {
		return DataTypeTranslator.toProperty(property, value);
	}

	public static byte[] serializeTypeAsString(Object value) {
		Type<?> type = DataTypeTranslator.getTypeMap().get(value.getClass());
		PropertyValue val = new PropertyValue();
		type.setPropertyValue(val, value);
		return val.getStringValueAsBytes();
	}

	public static String getNamespace(Query query) {
		return query.getAppIdNamespace().getNamespace();
	}

	public static Integer getOffset(FetchOptions fetchOptions) {
		return fetchOptions.getOffset();
	}

	public static String getFullTextSearch(Query query) {
		return query.getFullTextSearch();
	}

	public static Reference convertToPb(Key key) {
		return KeyTranslator.convertToPb(key);
	}

	public static Key createFromPb(Reference ref) {
		return KeyTranslator.createFromPb(ref);
	}

	public static CompiledCursor convertToPb(Cursor cursor) {
		return cursor.convertToPb();
	}

	public static Index newIndex(long id, String kind, boolean isAncestor,
			List<com.google.appengine.api.datastore.Index.Property> properties) {
		return new Index(id, kind, isAncestor, properties);
	}
}
